package com.sq.partner.manager.util;

import java.io.IOException;
import java.util.HashMap;

import javax.management.Attribute;
import javax.management.AttributeNotFoundException;
import javax.management.InstanceNotFoundException;
import javax.management.InvalidAttributeValueException;
import javax.management.MBeanException;
import javax.management.MBeanServerConnection;
import javax.management.MalformedObjectNameException;
import javax.management.ObjectName;
import javax.management.ReflectionException;
import javax.management.remote.JMXConnector;
import javax.management.remote.JMXConnectorFactory;
import javax.management.remote.JMXServiceURL;

/**
 * JMX工具类
 * 
 */
public class JMXUtils {

	public static final String JMXUSER = ReadProperties.map.get("JmxUser").toString();
	public static final String JMXPWD =ReadProperties.map.get("JmxPwd").toString();
	
	/**
	 * JMX方式调用方法
	 * 
	 * @param url
	 *            serviceURL
	 * @param objName
	 *            对象名
	 * @param operationName
	 *            方法名
	 * @param params
	 *            参数
	 * @param signature
	 *            参数类型
	 * @return 方法返回值
	 * @throws IOException
	 * @throws MalformedObjectNameException
	 * @throws InstanceNotFoundException
	 * @throws MBeanException
	 * @throws ReflectionException
	 */
	public static Object invoke(String url, String objName,
			String operationName, Object[] params, String[] signature)
			throws IOException, MalformedObjectNameException,
			InstanceNotFoundException, MBeanException, ReflectionException {
		
		JMXServiceURL serviceURL = new JMXServiceURL(url);
		JMXConnector connector = JMXConnectorFactory.connect(serviceURL,getCredentials());
		
		MBeanServerConnection server = connector.getMBeanServerConnection();
		ObjectName objectName = new ObjectName(objName);
		Object result = server.invoke(objectName, operationName, params,signature);
		connector.close();
		return result;
	}

	/**
	 * 获取属性值
	 * 
	 * @param url
	 *            serviceURL
	 * @param objName
	 *            对象名
	 * @param attributeName
	 *            属性名
	 * @return 属性值
	 * @throws IOException
	 * @throws MalformedObjectNameException
	 * @throws AttributeNotFoundException
	 * @throws InstanceNotFoundException
	 * @throws MBeanException
	 * @throws ReflectionException
	 */
	public static Object getAttribute(String url, String objName,
			String attributeName) throws IOException,
			MalformedObjectNameException, AttributeNotFoundException,
			InstanceNotFoundException, MBeanException, ReflectionException {
		JMXServiceURL serviceURL = new JMXServiceURL(url);
		JMXConnector connector = JMXConnectorFactory.connect(serviceURL,getCredentials());
		MBeanServerConnection server = connector.getMBeanServerConnection();
		ObjectName objectName = new ObjectName(objName);
		Object result = server.getAttribute(objectName, attributeName);
		connector.close();
		return result;
	}

	/**
	 * 设置属性值
	 * 
	 * @param url
	 *            serviceURL
	 * @param objName
	 *            对象名
	 * @param attributeName
	 *            属性名
	 * @param value
	 *            属性值
	 * @throws IOException
	 * @throws MalformedObjectNameException
	 * @throws InstanceNotFoundException
	 * @throws AttributeNotFoundException
	 * @throws InvalidAttributeValueException
	 * @throws MBeanException
	 * @throws ReflectionException
	 */
	public static void setAttribute(String url, String objName,String attributeName, Object value) throws IOException,MalformedObjectNameException, InstanceNotFoundException,
			AttributeNotFoundException, InvalidAttributeValueException,
			MBeanException, ReflectionException {
		JMXServiceURL serviceURL = new JMXServiceURL(url);
		JMXConnector connector = JMXConnectorFactory.connect(serviceURL,getCredentials());
		MBeanServerConnection server = connector.getMBeanServerConnection();
		ObjectName objectName = new ObjectName(objName);
		server.setAttribute(objectName, new Attribute(attributeName, value));
		connector.close();
	}

	private static HashMap<String,String[]> getCredentials(){
		HashMap<String,String[]> env = new HashMap<String,String[]>();
		String[] credentials = new String[] { JMXUSER, JMXPWD };
		env.put("jmx.remote.credentials", credentials);
		return env;
	}
	private JMXUtils() {
	}
}

